//
//  CocoaSynthAppDelegate.m
//  CocoaSynth
//
//  Created by Jérémy Brault on 06/02/10.
//  Copyright 2010 None. All rights reserved.
//

#import "CocoaSynthAppDelegate.h"
#import "System.h"
#import "PortInProtocol.h"
#import "PortOutProtocol.h"
#import "ConnectorProtocol.h"
#import "SequencerProtocol.h"
#import "ModuleKeyboard.h"
#import "ModuleVCO.h"
#import "ModuleVCOProtocol.h"
#import "ModuleVCF.h"
#import "ModuleVCFProtocol.h"
#import "ModuleVCA.h"
#import "ModuleSpeaker.h"
#import "PortIn.h"
#import "PortOut.h"
#import "Connector.h"
#import "Sequencer.h"
#import "SignalVisualizer.h"
#import "AudioWrapper.h"
#import "SinusoidGenerator.h"
#import "SquareGenerator.h"
#import "SawtoothGenerator.h"
#import "IdentityComputingStrategy.h"
#import "LowpassFilter.h"
#import "Factory.h"
#import "ADSRGenerator.h"
#import "ModuleADSR.h"
#import "VCAAlgorithm.h"
#import "ModuleVCA.h"
#import "BankViewController.h"
#import "MixingViewController.h"
#import "KeyboardViewController.h"
#import "FactoryProtocol.h"
#import "Factory.h"
#import "ModuleWriterProtocol.h"
#import "ModuleWriter.h"
#import "ParametersViewController.h"

@implementation CocoaSynthAppDelegate

#pragma mark - 
#pragma mark Accessors

@synthesize window;
@synthesize bank;
@synthesize mixingTable;
@synthesize keyboard;
@synthesize parameters;

#pragma mark -
#pragma mark NSApplicationDelegate

- (void)applicationDidFinishLaunching:(NSNotification*)aNotification
{
	sequencer = [[Sequencer alloc] init];
	[sequencer setIsStarted:NO];
	// IHM initialisation 
	BankViewController* bankController = [[BankViewController alloc] initWithNibName:@"BankTable" bundle:nil];
	[self.bank setDocumentView:bankController.view];
	
	MixingViewController* mixingController = [[MixingViewController alloc] init];
	[self.mixingTable setDocumentView:mixingController.view];
	
	KeyboardViewController* keyboardController = [[KeyboardViewController alloc] initWithNibName:@"KeyboardTable" bundle:nil];
	[self.keyboard addSubview:keyboardController.view];
	
	ParametersViewController* parametersController = [[ParametersViewController alloc] initWithNibName:@"ParametersTable" bundle:nil];
	[parameters setContentView:parametersController.view];
	
	[mixingController setParameters:parametersController];
	[mixingController setSequencer:sequencer];
	[mixingController setKeyboard:keyboardController];
	
	SignalVisualizer* visualizer = [[SignalVisualizer sharedSignalVisualizer] init];
	
	[display addSubview:visualizer.view];
	
	[window setAcceptsMouseMovedEvents:YES];
}

- (IBAction)openAction:(id)sender
{
	started = NO;
	[[AudioWrapper sharedWrapper] openOutput];
}

- (IBAction)startStopAction:(id)sender
{
	if (started) {
		[[AudioWrapper sharedWrapper] stopOutput];
		started = NO;
		[sequencer setIsStarted:NO];
	}
	else {
		[[AudioWrapper sharedWrapper] startOutput];
		started = YES;
		[sequencer setIsStarted:YES];
		[NSThread detachNewThreadSelector:@selector(start)
								 toTarget:sequencer
							   withObject:nil];
	}

}

- (IBAction)start:(id)sender
{
	if (!started) {
		[buttonStop setEnabled:YES];
		[buttonStart setEnabled:NO];
		[[AudioWrapper sharedWrapper] openOutput];
		[[AudioWrapper sharedWrapper] startOutput];
		[sequencer setIsStarted:YES];
		[NSThread detachNewThreadSelector:@selector(start)
								 toTarget:sequencer
							   withObject:nil];
		[[SignalVisualizer sharedSignalVisualizer] setStarted:YES];
		[NSThread detachNewThreadSelector:@selector(displayOutputSignal)
								 toTarget:[SignalVisualizer sharedSignalVisualizer]
							   withObject:nil];
		started = YES;
	}
}

- (IBAction)stop:(id)sender
{
	if (started) {
		[buttonStop setEnabled:NO];
		[buttonStart setEnabled:YES];
		[[AudioWrapper sharedWrapper] stopOutput];
		[[AudioWrapper sharedWrapper] closeOutput];
		[sequencer stop];
		[[SignalVisualizer sharedSignalVisualizer] setStarted:NO];
		started = NO;
	}
}

- (IBAction)closeAction:(id)sender
{
	[[AudioWrapper sharedWrapper] closeOutput];
	started = NO;
}

- (IBAction)displayBigBufferAction:(id)sender
{
	/*
	NSArray* a = [[AudioWrapper sharedWrapper] buffer]; 
	[[SignalVisualizer sharedSignalVisualizer] setSignal:a];
	//*/
	//*
	[NSThread detachNewThreadSelector:@selector(displayOutputSignal)
							 toTarget:[SignalVisualizer sharedSignalVisualizer]
						   withObject:nil];
	//*/
}

- (IBAction)pianoAction:(id)sender
{
	System* s = [System sharedSystem];
	float actualTime = [s time];
	[s setADSRStartTime:actualTime+0.01];
	[s setSustainDuration:5];
}

@end
